#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
const int N = 5e4 + 10;
typedef pair<int, int> PII;
map<PII, int>arr;
map<PII, int>sum;
int n;
int main()
{
	scanf("%d", &n);
	int mx = -100;
	int my = -100;
	for (int i = 0; i < n; i++) {
		int a, b, w;
		scanf("%d%d%d", &a, &b, &w);
		a++;
		b++;
		mx = max(mx, a);
		my = max(my, b);
		arr[{a, b}] += w;
	}
	for (int i = 1; i <= mx; i++) {
		for (int j = 1; j <= my; j++) {
			sum[{i, j}] = sum[{i - 1, j}] + sum[{i, j - 1}] - sum[{i - 1, j - 1}] + arr[{i, j}];
		}
	}
	int q;
	cin >> q;
	while (q--) {
		int x1, y1, x2, y2;
		scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
		x1++, y1++, x2++, y2++;
		printf("%d\n", sum[{x2, y2}] - sum[{x2, y1 - 1}] - sum[{x1 - 1, y2}] + sum[{x1 - 1, y1 - 1}]);
	}
	return 0;
}